package com.example.roundcornerprogressbar.slice;

import com.example.roundcornerprogressbar.MyApplication;
import com.example.roundcornerprogressbar.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.aafwk.ability.fraction.FractionScheduler;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TabList;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class FractionAbilitySlice extends AbilitySlice {
    private HiLogLabel label = new HiLogLabel(HiLog.LOG_APP,1, "FractionAbilitySlice");

    private TabList tabList;
    private FractionManager fractionManager;
    private FractionScheduler fractionScheduler;
    private SimpleFraction simpleFraction;
    private TextFraction textFraction;
    private IndeterminateFraction indeterminateFraction;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_fraction);
        initView();
    }

    private void initView(){
        tabList = (TabList)findComponentById(ResourceTable.Id_tab_list);
        initTabList();
        //initTabContent();
        tabList.selectTabAt(0);
    }

    private void initTabContent() {
        fractionManager = ((FractionAbility) getAbility()).getFractionManager();
        fractionScheduler = fractionManager.startFractionScheduler();
        simpleFraction = new SimpleFraction();
        fractionScheduler.add(ResourceTable.Id_tab_content, simpleFraction);
        textFraction = new TextFraction();
        fractionScheduler.add(ResourceTable.Id_tab_content, textFraction);
        indeterminateFraction = new IndeterminateFraction();
        fractionScheduler.add(ResourceTable.Id_tab_content, indeterminateFraction);
        fractionScheduler.submit();
    }

    private void initTabList() {
        addTab(tabList, "simple");
        addTab(tabList, "Text");
        addTab(tabList, "Indeterminate");
        //tabList.setFixedMode(true);

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                onTabSelected(tab.getPosition());
            }

            @Override
            public void onUnselected(TabList.Tab tab) {
            }

            @Override
            public void onReselected(TabList.Tab tab) {
            }
        });
    }

    private void onTabSelected(int position) {
        HiLog.info(label, "tab onSelected, position = " +position);
        fractionManager = ((FractionAbility) getAbility()).getFractionManager();
        fractionScheduler = fractionManager.startFractionScheduler();

        switch (position) {
            case 0:
//                if (simpleFraction == null) {
//                    simpleFraction = new SimpleFraction();
//                    fractionScheduler.add(ResourceTable.Id_tab_content, simpleFraction);
//                }
//                hideFraction();
//                fractionScheduler.show(simpleFraction);

                fractionScheduler.replace(ResourceTable.Id_tab_content, new SimpleFraction());
                break;
            case 1:
//                if (textFraction == null) {
//                    textFraction = new TextFraction();
//                    fractionScheduler.add(ResourceTable.Id_tab_content, textFraction);
//                }
//                hideFraction();
//                fractionScheduler.show(textFraction);

                fractionScheduler.replace(ResourceTable.Id_tab_content, new TextFraction());
                break;
            case 2:
//                if (indeterminateFraction == null) {
//                    indeterminateFraction = new IndeterminateFraction();
//                    fractionScheduler.add(ResourceTable.Id_tab_content, indeterminateFraction);
//                }
//                hideFraction();
//                fractionScheduler.show(indeterminateFraction);

                fractionScheduler.replace(ResourceTable.Id_tab_content, new IndeterminateFraction());
                break;
        }
        fractionScheduler.submit();
    }

    private void hideFraction() {
        if (null != simpleFraction) {
            fractionScheduler.hide(simpleFraction);
        }
        if (null != textFraction) {
            fractionScheduler.hide(textFraction);
        }
        if (null != indeterminateFraction) {
            fractionScheduler.hide(indeterminateFraction);
        }
    }

    private void addTab(TabList tabList, String text) {
        TabList.Tab tab = tabList.new Tab(getContext());
        tab.setText(text);
        tab.setMinWidth(100);
        tab.setPadding(12, 0, 12, 0);
        tabList.addTab(tab);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
